const { Lipei } = require("../models/lipeiMOdel");
const { moveFiles, removeFiles } = require("../utils/upload");
//上传理赔信息
async function addLipei(req, res) {
    const { clientName, insuranceName, insurancePirce, imgname, payment, reason, timeLength, salesId } = req.body;
    try {
        await Lipei.create({ clientName, insuranceName, insurancePirce, imgname, payment, reason, timeLength, salesId });
        //移动图片
        if (imgname) {

            moveFiles({
                formPath: './public/temp',
                toPath: './public/images',
                filename: imgname
            });
            removeFiles('./public/temp');
        }
        res.send({
            message: "新增理赔成功",
            status: 1
        });
    } catch (error) {
        console.log('新增理赔失败', error);
        res.send({
            message: '新增理赔失败',
            status: 0,
        });
    }
};
//获取所有理赔信息
async function getAllLipei(req, res) {
    const { pageSize, currentPage, value } = req.query;
    console.log('val', value);
    //总条数
    const total = await Lipei.countDocuments();
    //总页数
    const totalPages = Math.ceil(total / pageSize);
    try {
        let data;
        if (value) {
            data = await Lipei
                .find({
                    status: value
                })
                .sort({status:1})//排序查找
                .limit(pageSize - 0) //设置请求的条数
                .skip((currentPage - 1) * pageSize) //设置跳过的条数
                .populate('salesId')
        } else {
            data = await Lipei
                .find()
                .sort({status:1})//排序查找
                .limit(pageSize - 0) //设置请求的条数
                .skip((currentPage - 1) * pageSize) //设置跳过的条数
                .populate('salesId')
        }
        res.send({
            status: 1,
            message: '获取理赔成功',
            data: { total, totalPages, data, pageSize, currentPage }
        })
    } catch (error) {
        console.log('获取理赔失败', error);
        res.send({
            status: 0,
            message: '获取理赔信息成功'
        })
    };
}
//获取某一个销售员下面的理赔信息
async function getSaleLipei(req, res) {
    //传入一个销售Id去获取销售下的理赔信息
    const { pageSize = 5, currentPage = 1, id } = req.query;
    //总条数 
    const total = await (await Lipei.find({salesId: id})).length
    //总页数
    const totalPages = Math.ceil(total / pageSize);
    console.log(id);
    try {
        const data = await Lipei
            .find({ salesId: id })
            .limit(pageSize - 0) //设置请求的条数
            .skip((currentPage - 1) * pageSize) //设置跳过的条数
            .populate('salesId')
        res.send({
            status: 1,
            message: '获取理赔成功',
            data: { total, totalPages, data, pageSize, currentPage }
        })
    } catch (error) {
        console.log('获取理赔失败', error);
        res.send({
            status: 0,
            message: '获取理赔信息成功'
        })
    }
}         
//同意受理
async function agreeLipei(req, res) {
    const { _id } = req.body;
    const data = await Lipei
        .findById(_id)
        .update({ status: '受理成功' })//修改状态
    if (data) {
        res.send({
            status: 1,
            message: '理赔受理成功'
        })
    } else {
        res.send({
            status: 0,
            message: '理赔受理失败'
        })
    }
}
//撤回理赔信息
async function backLipei(req, res) {
    const { _id } = req.body;
    const data = await Lipei
        .findById(_id)
        .update({ status: '未受理' })//修改状态
    if (data) {
        res.send({
            status: 1,
            message: '理赔撤回消息成功'
        })
    } else {
        res.send({
            status: 0,
            message: '理赔受理失败'
        })
    }
}
//删除理赔
async function deleteOne(req, res) {
    const { _id } = req.body;
    const data = await Lipei.findByIdAndDelete(_id)
    // console.log(data);
    try {
        res.send({
            status: 1,
            message: '理赔删除成功'
        })
    } catch (error) {
        console.log('理赔删除失败', error);
        res.send({
            status: 0,
            message: '理赔删除失败'
        })
    }
}
//获取需要修改的理赔信息
async function getUpdataOneLipei(req, res) {
    const { _id } = req.query;
    // console.log(_id);
    const data = await Lipei.find({ _id });
    try {
        res.send({
            status: 1,
            message: '获取成功',
            data,
        })
    } catch (error) {
        console.log(error);
        res.send({
            status: 0,
            message: "获取失败"
        });
    }
}
//修改理赔信息
async function SureUpdataOneLipei(req, res) {
    const { _id, imgname, payment, reason,status } = req.body;
    const data = await Lipei.updateOne({ _id }, { imgname, payment, reason,status });
    try {
        if (data.nModified) {
            //移动图片
        if (imgname) {

            moveFiles({
                formPath: './public/temp',
                toPath: './public/images',
                filename: imgname
            });
            removeFiles('./public/temp');
        }
            res.send({
                status: 1,
                message: '修改成功'
            });
        } else {
            res.send({
                status: 0,
                message: '修改失败'
            });
        }
    } catch (error) {
        console.log(error);
    }
}
module.exports = { addLipei, getAllLipei, getSaleLipei, agreeLipei, backLipei, deleteOne, getUpdataOneLipei, SureUpdataOneLipei };
